<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <!-- 登录表单区域 -->
      <el-form ref="loginFormRef" :rules="loginFormRules" label-width="0px" :model="loginForm" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input type="text" v-model="loginForm.username" prefix-icon="iconfont icon-RectangleCopy"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-RectangleCopy1"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LoginView',
  data () {
    return {
      // 这是登录表单的数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 12, message: '用户名长度是4-12个字符之间', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度是6-15位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        // eslint-disable-next-line no-useless-return
        if (!valid) return
        // 判断是否登录
        // const result = await this.$http.post('', this.loginForm)
        // eslint-disable-next-line eqeqeq
        if (this.loginForm.username == 'admin' && this.loginForm.password == '123456') {
          // 提示成功
          this.$message.success('登录成功')
          // 保存token
          window.sessionStorage.setItem('token', 'admin123456')
          // 跳转到home页面
          await this.$router.push('/home')
        } else {
          this.$message.error('账号或者密码不正确')
        }
        // eslint-disable-next-line no-unused-vars
        // 登录逻辑判断
        // this.$message.success('登录成功')
      })
    }
  }

}
</script>

<style lang="less" scoped>
.login_container{
  height: 100%;
  background-color: #2b4b6b;
}

.login_box{
  width: 450px;
  height: 300px;
  background-color: #ffffff;
  border-radius: 3px;
  position: absolute;//移动
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);//移动到正中心
  .avatar_box{
    width: 130px;
    height: 130px;
    border: 1px solid #eeeeee;//边框
    border-radius: 50%;//圆角
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #ffffff;
    box-shadow: 0 0 10px #dddddd;//给边框加阴影
    img{
      width: 100%;
      height: 100%;
      border-radius: 50%;//圆角
      background-color: #eeeeee;
    }
  }
}
.btns{
  display: flex;
  justify-content: flex-end;//尾部对齐
}
.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
